/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package personal.dao;

import java.io.Serializable;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import personal.obj.OrderDetail;
import personal.utility.DBUtility;

/**
 *
 * @author PHATNBSE61057
 */
public class OrderDetailDAO implements Serializable {

    private final DBUtility utility = new DBUtility();

    public OrderDetailDAO() {
    }

    public boolean insertOrderDetail(OrderDetail od) {
        Connection con = null;
        PreparedStatement pst = null;
        int rowChanged;
        try {
            con = utility.makeConnection();
            String query = "INSERT INTO OrderDetail (OrderID, BookID, SubPrice, Quantity, UnitPrice) VALUES (?,?,?,?,?)";
            pst = con.prepareStatement(query);
            pst.setInt(1, od.getOrderID());
            pst.setString(2, od.getBookID());
            pst.setDouble(3, od.getSubPrice());
            pst.setInt(4, od.getQuantity());
            pst.setDouble(5, od.getUnitPrice());

            rowChanged = pst.executeUpdate();
            if (rowChanged > 0) {
                return true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }

    public List<OrderDetail> getOrderDetailByOrderID(int orderID) {
        Connection con = null;
        PreparedStatement pst = null;
        ResultSet rs = null;
        OrderDetail od = null;
        List<OrderDetail> result = new ArrayList<OrderDetail>();
        try {
            con = utility.makeConnection();
            String query = "SELECT * FROM OrderDetail WHERE OrderID = ?";
            pst = con.prepareStatement(query);
            pst.setInt(1, orderID);
            rs = pst.executeQuery();
            while (rs.next()) {
                int odID = rs.getInt("OrderID");
                String bookID = rs.getString("BookID");
                double subPrice = rs.getDouble("SubPrice");
                int quantity = rs.getInt("Quantity");
                double unitPrice = rs.getDouble("UnitPrice");

                od = new OrderDetail(odID, bookID, subPrice, quantity, unitPrice);
                result.add(od);
            }
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (rs != null) {
                    rs.close();
                }
                if (pst != null) {
                    pst.close();
                }
                if (con != null) {
                    con.close();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return null;
    }
}
